package top.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import top.api.common.ResponseResult;
import top.api.domain.dto.UserAddDto;
import top.api.domain.dto.UserUpdateDto;
import top.api.service.UserService;
import top.api.utils.RedisCache;

import java.util.List;

@RestController
@RequestMapping("system/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    @ApiOperation(value = "查询用户列表")
    @PreAuthorize("@ps.hasPermission('system:user:query')") // 查询用户 权限校验
    public ResponseResult list(@RequestParam Integer pageNum ,@RequestParam Integer pageSize,String userName, String phonenumber,String status){
        return userService.getUserList(pageNum,pageSize,userName,phonenumber,status);
    }

    @PostMapping
    @ApiOperation(value = "添加用户并绑定角色")
    @PreAuthorize("@ps.hasPermission('system:user:add')")
    public ResponseResult addUser(@RequestBody UserAddDto userAddDto){
        return userService.addUser(userAddDto);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "批量删除用户")
    @PreAuthorize("@ps.hasPermission('system:user:remove')")
    public ResponseResult deleteUsers(@PathVariable List<Long> ids){
        return userService.deleteUsers(ids);
    }

    /**
     * 用户信息回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询用户详细信息")
    @PreAuthorize("@ps.hasPermission('system:user:query')") // 查询用户 权限校验
    public ResponseResult getUserInfo(@PathVariable Long id){
        return userService.getUserInfo(id);
    }

    @PutMapping
    @ApiOperation(value = "修改用户")
    @PreAuthorize("@ps.hasPermission('system:user:edit')") // 修改用户 权限校验
    public ResponseResult updateUser(@RequestBody UserUpdateDto userUpdateDto){
        return userService.AdminUpdateUserInfo(userUpdateDto);
    }
}
